﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using NetCoreCMS.Common;
using NetCoreCMS.IServices;
using NetCoreCMS.Models;

namespace NetCoreCMS.Web.Areas.Payments.Controllers
{
    /// <summary>
    /// 现金余额支付
    /// </summary>
    public class CashController : AreasController
    {
        public IUser_rechargeService _user_recharge { get; set; }
        public IOrdersService _orders { get; set; }
        public ISite_paymentService _site_payment { get; set; }
        public IPaymentService _payment { get; set; }
        public IUser_point_logService _user_point_log { get; set; }
        public IUser_amount_logService _user_amount_log { get; set; }
        public IBillsService _bills { get; set; }
        public IActionResult Index()
        {
            //读取站点配置信息
            string order_no = CMSRequest.GetFormString("pay_order_no", HttpContext).ToUpper();
            Orders modelorders = _orders.GetModel(order_no);
            if (modelorders == null)
            {
                return ToError("对不起，订单详情获取出错,请重试！");
            }
            decimal order_amount = modelorders.order_amount;
            string subject = CMSRequest.GetFormString("pay_subject", HttpContext);
            if (order_no == "" || order_amount == 0)
            {
                return Content("对不起，您提交的参数有误！");
            }
            //检查是否已登录
            Users userModel = GetUserInfo();
            if (userModel == null)
            {
                return Redirect("/Web/Payment/Index?action=login"); //尚未登录
            }            
            if (userModel.cash_point <order_amount)
            {
                return Redirect("/Web/Payment/Index?action=recharge"); //账户的余额不足
            }

            if (order_no.StartsWith("B")) //B开头为商品订单
            {
                Orders model = _orders.GetModel(order_no);
                if (model == null)
                {
                    return ToError("对不起，商品订单号不存在！");
                }
                if (model.payment_status == 1)
                {
                    //执行扣取账户金额
                    int result = _user_amount_log.Add(userModel.id, userModel.user_name, -1 * order_amount, subject,1);
                    if (result > 0)
                    {
                        //更改订单状态
                        bool result1 = _orders.UpdateField(order_no, "status=2,payment_status=2,payment_time='" + DateTime.Now + "'");
                        if (!result1)
                        {
                            return Redirect("/Web/Payment/Index?action=error");
                        }
                        ////扣除积分
                        //if (model.point < 0)
                        //{
                        //    _user_point_log.Add(model.user_id, model.user_name, model.point, "换购扣除积分，订单号：" + model.order_no, false);
                        //}
                    }
                    else
                    {
                        return Redirect("/Web/Payment/Index?action=error");
                    }
                }
                //支付成功
                return Redirect("/Web/Payment/Index?action=succeed&order_no=" + order_no);
            }
            return ToError("对不起，找不到需要支付的订单类型！");
        }
    }
}